﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using STS.Common.Enums;

namespace STS.Common
{
    public class ConnectionInfo
    {
        public string ProviderName { get; private set; }

        public string ConnectionString { get; private set; }
        public ProviderType ProviderType { get; private set; }

        public ConnectionInfo(string connectionString)
            : this(connectionString, null)
        {

        }

        public ConnectionInfo(string connectionString, string providerName)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new STSException("Unknown connection string.");
            }
            if (string.IsNullOrWhiteSpace(providerName))
            {
                var csb = new DbConnectionStringBuilder { ConnectionString = connectionString };

                if (csb.ContainsKey("provider"))
                {
                    providerName = csb["provider"].ToString();
                }
            }

            if (string.IsNullOrWhiteSpace(providerName))
            {
                throw new STSException("Unknown connection provider.");
            }

            ConnectionString = connectionString;
            ProviderName = providerName;
            switch (providerName)
            {
                case "System.Data.SqlClient":
                    ProviderType = Enums.ProviderType.MSSql;
                    break;
                default:
                    break;
            }
        }
    }
}
